import 'package:flutter/material.dart';
import 'package:mobile_app.core/core/presentation/theme/styles.dart';
import 'package:mobile_app.home.core/core/presentation/theme/home_core_styles.dart';

import 'home_core_text_theme.dart';

class HomeCoreTheme extends InheritedTheme {
  HomeCoreTheme({
    required Brightness brightness,
    required Color primaryColor,
    required super.child,
    super.key,
  })  : _brightness = brightness,
        _primaryColor = primaryColor,
        _data = _buildThemeData(
          brightness: brightness,
          primaryColor: primaryColor,
        );

  final Brightness _brightness;
  final Color _primaryColor;
  final HomeCoreThemeData _data;

  static HomeCoreThemeData _buildThemeData({
    required Brightness brightness,
    required Color primaryColor,
  }) {
    return switch (brightness) {
      Brightness.dark => HomeCoreThemeData.dark(
        primaryColor: primaryColor,
      ),
      Brightness.light => HomeCoreThemeData.light(
        primaryColor: primaryColor,
      ),
    };
  }

  static HomeCoreThemeData of(BuildContext context) {
    final inheritedTheme = context.dependOnInheritedWidgetOfExactType<HomeCoreTheme>()!;
    return inheritedTheme._data;
  }

  @override
  Widget wrap(BuildContext context, Widget child) {
    final ancestorTheme = context.findAncestorWidgetOfExactType<HomeCoreTheme>();

    if (identical(this, ancestorTheme)) {
      return child;
    }

    return HomeCoreTheme(
      brightness: _brightness,
      primaryColor: _primaryColor,
      child: child,
    );
  }

  @override
  bool updateShouldNotify(HomeCoreTheme oldWidget) {
    return _brightness != oldWidget._brightness || _primaryColor != oldWidget._primaryColor;
  }
}

class HomeCoreThemeData {
  const HomeCoreThemeData._internal({
    required this.cardColor,
    required this.cardButtonColor,
    required this.lexicalItemCardShadows,
    required this.cardCorrectAnswerTextColor,
    required this.translationSelectionCardCorrectAnswerButtonColor,
    required this.searchBarColor,
    required this.searchBarPlaceholderColor,
    required this.textTheme,
  });

  factory HomeCoreThemeData.light({
    required Color primaryColor,
  }) {
    return HomeCoreThemeData._internal(
      cardColor: Colors.white,
      cardButtonColor: CoreStyles.lightGrey3,
      lexicalItemCardShadows: [
        const BoxShadow(
          color: CoreStyles.lightGrey5,
          blurRadius: 5.0,
          spreadRadius: 1.0,
        ),
      ],
      cardCorrectAnswerTextColor: HomeCoreStyles.cardCorrectAnswerTextColor,
      translationSelectionCardCorrectAnswerButtonColor:
          HomeCoreStyles.translationSelectionCardCorrectAnswerButtonColor,
      searchBarColor: CoreStyles.lightGrey2,
      searchBarPlaceholderColor: CoreStyles.grey2,
      textTheme: HomeCoreTextTheme.light(
        primaryColor: primaryColor,
      ),
    );
  }

  factory HomeCoreThemeData.dark({
    required Color primaryColor,
  }) {
    return HomeCoreThemeData._internal(
      cardColor: CoreStyles.darkGrey4,
      cardButtonColor: CoreStyles.darkGrey3,
      lexicalItemCardShadows: [
        const BoxShadow(
          color: Colors.black,
          blurRadius: 5.0,
          spreadRadius: 1.0,
        ),
      ],
      cardCorrectAnswerTextColor: HomeCoreStyles.cardCorrectAnswerTextColor,
      translationSelectionCardCorrectAnswerButtonColor:
          HomeCoreStyles.translationSelectionCardCorrectAnswerButtonColor,
      searchBarColor: CoreStyles.darkGrey3,
      searchBarPlaceholderColor: CoreStyles.grey2,
      textTheme: HomeCoreTextTheme.dark(
        primaryColor: primaryColor,
      ),
    );
  }

  final Color cardColor;
  final Color cardButtonColor;
  final List<BoxShadow> lexicalItemCardShadows;
  final Color cardCorrectAnswerTextColor;
  final Color translationSelectionCardCorrectAnswerButtonColor;
  final Color searchBarColor;
  final Color searchBarPlaceholderColor;
  final HomeCoreTextTheme textTheme;
}
